package com.sooncode.project.focusservice.model;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.alibaba.excel.enums.BooleanEnum;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import com.alibaba.excel.enums.poi.VerticalAlignmentEnum;
import com.sooncode.project.core.finder.Finder;
import com.sooncode.project.core.finder.OType;
import lombok.Data;

import java.util.*;

@Data
@HeadRowHeight(30)
@ContentRowHeight(150)
public class CourseExcel {
    @ExcelProperty(value = "阶段",index = 0)
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER,verticalAlignment = VerticalAlignmentEnum.CENTER)
    private String phase;
    @ExcelProperty(value = "听觉基础",index = 1)
    @ContentStyle(wrapped = BooleanEnum.TRUE,verticalAlignment = VerticalAlignmentEnum.CENTER)
    @ColumnWidth(100)
    private String hearing;
    @ExcelProperty(value = "视觉基础",index = 2)
    @ContentStyle(wrapped = BooleanEnum.TRUE,verticalAlignment = VerticalAlignmentEnum.CENTER)
    @ColumnWidth(100)
    private String vision;


    public List<CourseExcel> export(Test test) {
        List<CourseExcel> list=new ArrayList<>();
        List<CourseSetting> courseSettingList=new Finder<>(CourseSetting.class,false)
                .byField("age",Test.getTestAge(test.getAge()))
                .and("minScore",test.getAverageScore(), OType.lte)
                .and("maxScore",test.getAverageScore(), OType.gte)
                .list();

        for(CourseSetting setting:courseSettingList){
            Map<String,List> courseNameMap= new HashMap<>();
            for(CourseSettingItem item:setting.getItems()) {
                for(String s:item.getCourseNames()){
                    String[] name=s.split("：");
                    if(name.length>1){
                        if(!courseNameMap.containsKey(name[0]))
                            courseNameMap.put(name[0],new ArrayList<>());
                        courseNameMap.get(name[0]).add(name[1]);
                    }
                    else{
                        if(!courseNameMap.containsKey(""))
                            courseNameMap.put("",new ArrayList<>());
                        courseNameMap.get("").add(s);
                    }

                }
                String courseNames="";
                for(String name:courseNameMap.keySet()){
                    if(name.equals("")) continue;
                        courseNames+=(name+"："+String.join("，",courseNameMap.get(name))+"\n");
                }
                if(courseNameMap.containsKey(""))
                    courseNames+=String.join("，",courseNameMap.get(""))+"\n";
                CourseExcel courseExcel = list.stream().filter(f->f.phase.equals(item.getPhase())).findFirst().orElse(null);
                if(courseExcel==null) {
                    courseExcel = new CourseExcel();
                    courseExcel.setPhase(item.getPhase());
                    list.add(courseExcel);
                }
                if(setting.getTestType().equals("听觉"))
                    courseExcel.setHearing(courseNames);
                else if(setting.getTestType().equals("视觉"))
                    courseExcel.setVision(courseNames);
            }

        }

        return list;
    }
}
